local shenji = fk.CreateSkill{
  name = "kl__shenji",
  tags = { Skill.Permanent },
}

shenji:addEffect("viewas", {
  pattern = "slash",
  prompt = "#kl__shenji",
  anim_type = "offensive",
  mute_card = false,
  interaction = function(self, player)
    local all_names = table.filter(Fk:getAllCardNames("b"), function(name)
      return Fk:cloneCard(name).trueName == "slash"
    end)
    local names = player:getViewAsCardNames(shenji.name, all_names)
    if #names == 0 then return end
    return UI.CardNameBox { choices = names, all_choices = all_names }
  end,
  handly_pile = true,
  filter_pattern = function (self, player)
    local vs_pattern = {
      max_num = 1,
      min_num = 1,
      pattern = ".",
    }
    local targets = table.filter(Fk:currentRoom().alive_players, function (p)
      return #p:getSkillNameList() > 0 and p ~= player
    end)
    if #player:getSkillNameList() > 1 then  --飞刀判定
      table.insertIfNeed(targets, player)
    end
    if #targets > 0 then
      vs_pattern.min_num = 0
    end
    return vs_pattern
  end,
  before_use = function (self, player, use)
    if #use.card.subcards == 0 then
      local room = player.room
      local targets = table.filter(Fk:currentRoom().alive_players, function (p)
        return #p:getSkillNameList() > 0 and p ~= player
      end)
      if #player:getSkillNameList() > 1 then
        table.insertIfNeed(targets, player)
      end
      if #targets == 0 then return "" end
      local to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        prompt = "神技：选择一名角色，然后选择其的一个技能当作此杀！",
        targets = targets,
        skill_name = shenji.name,
        cancelable = false,
      })
      if #to > 0 then
        local target = to[1]
        local skills = table.filter(target:getSkillNameList(), function (s)
          if target == player then
            return s ~= shenji.name
          else
            return true
          end
        end)
        local chosen = room:askToChoice(player, {
          choices = skills,
          skill_name = shenji.name,
          prompt = "神技：选择其的一个技能当作此杀！",
          detailed = true,
          cancelable = false,
        })
        if chosen then
          use.extra_data = use.extra_data or {}
          use.extra_data.kl_shenji_skill = {
            player = player,
            target = target,
            skill = chosen,
            card = use.card,
          }
        end
      end
    end
  end,
  view_as = function(self, player, cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    if #cards > 0 then
      card:addSubcards(cards)
    end
    card.skillName = shenji.name
    return card
  end,
})

shenji:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(shenji.name, true) and data.extra_data and data.extra_data.kl_shenji_skill
  end,
  on_refresh = function (self, event, target, player, data)
    local from = data.extra_data.kl_shenji_skill.player
    local to = data.extra_data.kl_shenji_skill.target
    local skill = data.extra_data.kl_shenji_skill.skill
    local card = data.extra_data.kl_shenji_skill.card
    player.room:sendLog{
      type = "%to的技能【%arg】被 %from 当作 %arg2 使用了！",
      from = from.id,
      to = {to.id},
      arg = skill,
      arg2 = card:toLogString(),
    }
    player.room:handleAddLoseSkills(to, "-"..skill)
  end,
})

shenji:addEffect(fk.PreCardRespond, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(shenji.name, true) and data.extra_data and data.extra_data.kl_shenji_skill
  end,
  on_refresh = function (self, event, target, player, data)
    local from = data.extra_data.kl_shenji_skill.player
    local to = data.extra_data.kl_shenji_skill.target
    local skill = data.extra_data.kl_shenji_skill.skill
    local card = data.extra_data.kl_shenji_skill.card
    player.room:sendLog{
      type = "%to的技能【%arg】被 %from 当作 %arg2 使用了！",
      from = from.id,
      to = {to.id},
      arg = skill,
      arg2 = card:toLogString(),
    }
    player.room:handleAddLoseSkills(to, "-"..skill)
  end,
})

return shenji
